#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess

def main():
        delete_local = 'rm -rf ~/git/Social/Social/resource/like/output'
        subprocess.call(delete_local, shell=True)
         
        #delete output
        delete_output = '~/hadoop-2.6.0/bin/hdfs dfs -rm -r /Social/output'
        delete_input = '~/hadoop-2.6.0/bin/hdfs dfs -rm -r /Social/input'
        subprocess.call(delete_output, shell=True)
        subprocess.call(delete_input, shell=True)
         
        #mk input dir 
        mkdir = '~/hadoop-2.6.0/bin/hdfs dfs -mkdir /Social/input'
        subprocess.call(mkdir, shell=True)
        #put input file
        put_file = '~/hadoop-2.6.0/bin/hdfs dfs -put '\
                    '~/git/Social/Social/resource/like/temp_like.txt ' \
                    '/Social/input'
        subprocess.call(put_file, shell=True)
        #run
        run_hadoop = '''~/hadoop-2.6.0/bin/hadoop jar \
                        ~/hadoop-2.6.0/share/hadoop/tools/lib/hadoop-streaming*.jar \
                        -file ~/git/Social/Social/src/hadoop/like/mapper.py \
                        -mapper ~/git/Social/Social/src/hadoop/like/mapper.py \
                        -file ~/git/Social/Social/src/hadoop/like/reducer.py \
                        -reducer ~/git/Social/Social/src/hadoop/like/reducer.py \
                        -input /Social/input -output /Social/output
                    '''
        subprocess.call(run_hadoop, shell=True)
        #output
        get_file = '~/hadoop-2.6.0/bin/hdfs dfs -get /Social/output '\
                    '~/git/Social/Social/resource/like'
        subprocess.call(get_file, shell=True)
        
        sort_source = '/home/ivy/git/Social/Social/resource/like/output/part-00000'
        sort_target = '/home/ivy/git/Social/Social/resource/like/like_1000.txt'
        sort = subprocess.Popen("sort -t '\t' -k3 -nr ", shell=True,
                    stdin=open(sort_source, 'r'),
                    stdout=subprocess.PIPE,  # Not a pipe here,
                    )
        subprocess.Popen(" head -1000 ", shell=True,
                    stdin=sort.stdout,
                    stdout=open(sort_target, 'w'),  # Not a pipe here,
                    )
    
if __name__ == '__main__':
    main() 